package com.honglu.backend.controller;

import com.honglu.backend.entity.AppVersion;
import com.honglu.backend.service.impl.AppVersionServiceImpl;
import com.honglu.backend.utils.CodeUtils;
import com.honglu.backend.utils.JSONUtils;
import com.honglu.backend.vo.AppVersionVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by tangbin on 2017/6/16.
 */
@Controller
@RequestMapping("/backend/web/appVersion/")
public class AppVersionController {

    private static Logger logger = Logger.getLogger(AppVersionController.class);

    @Autowired
    private AppVersionServiceImpl appVersionService;


    /**
     * app版本集合
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getAppVersion.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void getAppVersion(HttpServletRequest request, HttpServletResponse response, AppVersionVo vo){
        try {

            //获取count
            Integer count = appVersionService.selectCount(vo);
            //获取统计list
            List<AppVersion> list = appVersionService.selectList(vo);
            JSONUtils.toListJSON(response, list, count, CodeUtils.QUERRYSUCCESS);
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            JSONUtils.toJSON(response, CodeUtils.QUERRYFAIL);
            return ;
        }
    }


    /**
     * 新增app版本集合
     * @param request
     * @param response
     */
    @RequestMapping(value = "/create.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void create(HttpServletRequest request, HttpServletResponse response, AppVersionVo vo){
        try {

            //新增app版本
            boolean flag = appVersionService.insert(vo);
            if (flag) {
                JSONUtils.toJSON(response, CodeUtils.SAVE_SUCCESS);
            } else {
                JSONUtils.toJSON(response, CodeUtils.SAVE_FAIL);
            }
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            JSONUtils.toJSON(response, CodeUtils.SAVE_FAIL);
            return ;
        }
    }

    /**
     * 删除app版本集合
     * @param request
     * @param response
     */
    @RequestMapping(value = "/delete.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void delete(HttpServletRequest request, HttpServletResponse response, String appVersionId){
        try {

            //删除app版本
            boolean flag = appVersionService.delete(appVersionId);
            if (flag) {
                JSONUtils.toJSON(response, CodeUtils.DELETE_SUCCESS);
            } else {
                JSONUtils.toJSON(response, CodeUtils.DELETE_FAIL);
            }
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            JSONUtils.toJSON(response, CodeUtils.DELETE_FAIL);
            return ;
        }
    }

    /**
     * 获取app版本实体
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getEntity.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void getEntity(HttpServletRequest request, HttpServletResponse response, String appVersionId){
        try {

            //获取app版本
            AppVersion appVersion = appVersionService.selectEntity(appVersionId);
            if (appVersion != null) {
                JSONUtils.toJSON(response, CodeUtils.QUERRYSUCCESS, appVersion);
            } else {
                JSONUtils.toJSON(response, CodeUtils.QUERRYFAIL);
            }
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            JSONUtils.toJSON(response, CodeUtils.QUERRYFAIL);
            return ;
        }
    }

    /**
     * 修改app版本集合
     * @param request
     * @param response
     */
    @RequestMapping(value = "/modify.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void modify(HttpServletRequest request, HttpServletResponse response, AppVersionVo vo){
        try {

            //新增app版本
            boolean flag = appVersionService.modify(vo);
            if (flag) {
                JSONUtils.toJSON(response, CodeUtils.UPDATE_SUCCESS);
            } else {
                JSONUtils.toJSON(response, CodeUtils.UPDATE_FAIL);
            }
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            JSONUtils.toJSON(response, CodeUtils.UPDATE_FAIL);
            return ;
        }
    }

}
